package com.obaamilos.accesscontrol;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.border.*;

public class LoginTestesGUI extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3361410698610844371L;
	
	private LoginTestes meuAgente;
	private JTextField txtLogin;
	private JPasswordField txtSenha;
	private JTextField txtRetornoLogin;
	private JTextField txtNome;
	private JTextField txtID;
	private JTextField txtAgAC;
	private JTextField txtAgQry;
	private JTextField txtAgUpd;
	private JTextField txtLocalTDB;
	private JList lstRecursos;
	private JList lstAcoes;
	private DefaultListModel lstRecursosModel;
	private JTextArea txtPerfisUsuario;
	private JTextArea txtRetornoSolicitacao;
	private JTextField txtToken;
	
	public JTextField getTxtLogin() {
		return txtLogin;
	}

	public void setTxtLogin(JTextField txtLogin) {
		this.txtLogin = txtLogin;
	}

	public JPasswordField getTxtSenha() {
		return txtSenha;
	}

	public void setTxtSenha(JPasswordField txtSenha) {
		this.txtSenha = txtSenha;
	}

	public JTextField getTxtToken() {
		return txtToken;
	}

	public void setTxtToken(JTextField txtToken) {
		this.txtToken = txtToken;
	}

	public JList getLstRecursos() {
		return lstRecursos;
	}

	public void setLstRecursos(JList lstRecursos) {
		this.lstRecursos = lstRecursos;
	}

	public DefaultListModel getLstRecursosModel() {
		return lstRecursosModel;
	}

	public void setLstRecursosModel(DefaultListModel lstRecursosModel) {
		this.lstRecursosModel = lstRecursosModel;
	}

	public JTextField getTxtRetornoLogin() {
		return txtRetornoLogin;
	}

	public void setTxtRetornoLogin(JTextField txtRetornoLogin) {
		this.txtRetornoLogin = txtRetornoLogin;
	}

	public JTextField getTxtAgAC() {
		return txtAgAC;
	}

	public void setTxtAgAC(JTextField txtAgAC) {
		this.txtAgAC = txtAgAC;
	}

	public JTextField getTxtAgQry() {
		return txtAgQry;
	}

	public void setTxtAgQry(JTextField txtAgQry) {
		this.txtAgQry = txtAgQry;
	}

	public JTextField getTxtAgUpd() {
		return txtAgUpd;
	}

	public void setTxtAgUpd(JTextField txtAgUpd) {
		this.txtAgUpd = txtAgUpd;
	}

	@SuppressWarnings("serial")
	LoginTestesGUI(LoginTestes lt) {
		
		meuAgente = lt;
		
		setBounds(100, 100, 674, 443);
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		getContentPane().add(tabbedPane, BorderLayout.CENTER);
		
		JPanel pnlLogin = new JPanel();
		tabbedPane.addTab("Login", null, pnlLogin, null);
		pnlLogin.setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
		panel.setBounds(10, 11, 624, 98);
		pnlLogin.add(panel);
		panel.setLayout(null);
		
		JLabel lblLogin = new JLabel("Login:");
		lblLogin.setFont(new Font("Dialog", Font.BOLD, 11));
		lblLogin.setHorizontalAlignment(SwingConstants.TRAILING);
		lblLogin.setBounds(10, 11, 52, 14);
		panel.add(lblLogin);
		
		JLabel lblSenha = new JLabel("Senha:");
		lblSenha.setFont(new Font("Dialog", Font.BOLD, 11));
		lblSenha.setHorizontalAlignment(SwingConstants.TRAILING);
		lblSenha.setBounds(10, 36, 52, 14);
		panel.add(lblSenha);
		
		txtLogin = new JTextField();
		txtLogin.setBounds(69, 8, 86, 20);
		panel.add(txtLogin);
		txtLogin.setColumns(10);
		
		txtSenha = new JPasswordField();
		txtSenha.setBounds(69, 33, 86, 20);
		panel.add(txtSenha);
		
		JButton btnLogin = new JButton("Login");
		btnLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				// Executa login
				String mPwd = new String(txtSenha.getPassword());
				txtRetornoLogin.setText(meuAgente.realizaLogin(txtLogin.getText(), mPwd));
			}
		});
		btnLogin.setBounds(66, 65, 89, 24);
		panel.add(btnLogin);
		
		JLabel lblNewLabel = new JLabel("Retorno:");
		lblNewLabel.setFont(new Font("Dialog", Font.BOLD, 11));
		lblNewLabel.setHorizontalAlignment(SwingConstants.TRAILING);
		lblNewLabel.setBounds(173, 11, 64, 14);
		panel.add(lblNewLabel);
		
		txtRetornoLogin = new JTextField();
		txtRetornoLogin.setEditable(false);
		txtRetornoLogin.setBounds(243, 8, 375, 20);
		panel.add(txtRetornoLogin);
		txtRetornoLogin.setColumns(10);
		
		JButton btnLogout = new JButton("Logout");
		btnLogout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				meuAgente.realizaLogout(txtRetornoLogin.getText());
				txtRetornoLogin.setText("");
				txtID.setText("");
				txtNome.setText("");
				txtPerfisUsuario.setText("");
				txtToken.setText("");
				txtLogin.setText("");
				txtSenha.setText("");
			}
		});
		btnLogout.setBounds(165, 65, 89, 24);
		panel.add(btnLogout);
		
		JPanel panel_2 = new JPanel();
		panel_2.setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
		panel_2.setBounds(10, 120, 233, 86);
		pnlLogin.add(panel_2);
		panel_2.setLayout(null);
		
		JLabel lblNewLabel_8 = new JLabel("Agentes AccessControl:");
		lblNewLabel_8.setFont(new Font("Dialog", Font.BOLD, 11));
		lblNewLabel_8.setBounds(10, 11, 161, 14);
		panel_2.add(lblNewLabel_8);
		
		JLabel lblNewLabel_9 = new JLabel("Agentes Query:");
		lblNewLabel_9.setFont(new Font("Dialog", Font.BOLD, 11));
		lblNewLabel_9.setBounds(10, 36, 161, 14);
		panel_2.add(lblNewLabel_9);
		
		JLabel lblNewLabel_10 = new JLabel("Agentes Update:");
		lblNewLabel_10.setFont(new Font("Dialog", Font.BOLD, 11));
		lblNewLabel_10.setBounds(10, 61, 161, 14);
		panel_2.add(lblNewLabel_10);
		
		txtAgAC = new JTextField();
		txtAgAC.setEditable(false);
		txtAgAC.setText("0");
		txtAgAC.setHorizontalAlignment(SwingConstants.CENTER);
		txtAgAC.setBounds(181, 8, 45, 20);
		panel_2.add(txtAgAC);
		txtAgAC.setColumns(10);
		
		txtAgQry = new JTextField();
		txtAgQry.setEditable(false);
		txtAgQry.setText("0");
		txtAgQry.setHorizontalAlignment(SwingConstants.CENTER);
		txtAgQry.setBounds(181, 33, 45, 20);
		panel_2.add(txtAgQry);
		txtAgQry.setColumns(10);
		
		txtAgUpd = new JTextField();
		txtAgUpd.setText("0");
		txtAgUpd.setEditable(false);
		txtAgUpd.setHorizontalAlignment(SwingConstants.CENTER);
		txtAgUpd.setBounds(181, 58, 45, 20);
		panel_2.add(txtAgUpd);
		txtAgUpd.setColumns(10);
		
		JPanel panel_3 = new JPanel();
		panel_3.setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
		panel_3.setBounds(259, 120, 375, 86);
		pnlLogin.add(panel_3);
		panel_3.setLayout(null);
		
		JLabel lblNewLabel_11 = new JLabel("Local TDB:");
		lblNewLabel_11.setBounds(10, 11, 137, 14);
		panel_3.add(lblNewLabel_11);
		
		txtLocalTDB = new JTextField();
		txtLocalTDB.setText("../../TDBTESTE/TDB1");
		txtLocalTDB.setBounds(10, 26, 360, 20);
		panel_3.add(txtLocalTDB);
		txtLocalTDB.setColumns(10);
		
		JButton btnSetaConfig = new JButton("Envia Configura\u00E7\u00E3o");
		btnSetaConfig.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				meuAgente.configuraLocalTDB(txtLocalTDB.getText());
			}
		});
		btnSetaConfig.setBounds(10, 52, 187, 24);
		panel_3.add(btnSetaConfig);
		
		JPanel pnlAcoes = new JPanel();
		tabbedPane.addTab("A\u00E7\u00F5es", null, pnlAcoes, null);
		pnlAcoes.setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 25, 245, 345);
		pnlAcoes.add(scrollPane);
		
		lstRecursosModel = new DefaultListModel();
		lstRecursos = new JList(lstRecursosModel);
		lstRecursos.setFont(new Font("Dialog", Font.BOLD, 11));
		lstRecursos.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		scrollPane.setViewportView(lstRecursos);
		
		JLabel lblNewLabel_1 = new JLabel("Recursos:");
		lblNewLabel_1.setBounds(10, 11, 106, 14);
		pnlAcoes.add(lblNewLabel_1);
		
		JLabel lblNewLabel_2 = new JLabel("A\u00E7\u00F5es:");
		lblNewLabel_2.setBounds(265, 11, 65, 14);
		pnlAcoes.add(lblNewLabel_2);
		
		lstAcoes = new JList();
		lstAcoes.setFont(new Font("Dialog", Font.BOLD, 11));
		lstAcoes.setModel(new AbstractListModel() {
			String[] values = new String[] {"Control", "Read", "Write", "Delete", "Execute"};
			public int getSize() {
				return values.length;
			}
			public Object getElementAt(int index) {
				return values[index];
			}
		});
		lstAcoes.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		lstAcoes.setBounds(265, 26, 65, 91);
		pnlAcoes.add(lstAcoes);
		
		JButton btnSolicitar = new JButton("Solicitar");
		btnSolicitar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				// Solicita acesso...
				if (!lstRecursos.isSelectionEmpty()) {
					String mRecur;
					String [] mRecurs = lstRecursos.getSelectedValue().toString().split(" - ");
					if (mRecurs.length > 1) {
						mRecur = mRecurs[1];
						mRecur = mRecur.replace("]", "");
					}
					else {
						mRecur = lstRecursos.getSelectedValue().toString();
						mRecur = mRecur.replace("[", "");
						mRecur = mRecur.replace("]", "");
					}
					if (!lstAcoes.isSelectionEmpty()) {
						String mAcao = lstAcoes.getSelectedValue().toString();
						mAcao = meuAgente.EnviaSolicitacaoAcesso("ACCESS<aa>"+txtRetornoLogin.getText()+"<aa>"+mRecur+"<aa>"+mAcao);
						if (mAcao.length() > 15) {
							txtRetornoSolicitacao.setText("Permitido!");
						}
						else {
							txtRetornoSolicitacao.setText("Não Permitido!");
						}
					}
				}
			}
		});
		btnSolicitar.setBounds(340, 23, 127, 24);
		pnlAcoes.add(btnSolicitar);
		
		JLabel lblNewLabel_3 = new JLabel("Retorno:");
		lblNewLabel_3.setBounds(340, 57, 75, 14);
		pnlAcoes.add(lblNewLabel_3);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
		panel_1.setBounds(265, 219, 384, 151);
		pnlAcoes.add(panel_1);
		panel_1.setLayout(null);
		
		JLabel lblNewLabel_5 = new JLabel("ID:");
		lblNewLabel_5.setHorizontalAlignment(SwingConstants.TRAILING);
		lblNewLabel_5.setBounds(10, 11, 46, 14);
		panel_1.add(lblNewLabel_5);
		
		JLabel lblNewLabel_6 = new JLabel("Nome:");
		lblNewLabel_6.setHorizontalAlignment(SwingConstants.TRAILING);
		lblNewLabel_6.setBounds(10, 36, 46, 14);
		panel_1.add(lblNewLabel_6);
		
		txtNome = new JTextField();
		txtNome.setEditable(false);
		txtNome.setBounds(63, 33, 315, 20);
		panel_1.add(txtNome);
		txtNome.setColumns(10);
		
		txtID = new JTextField();
		txtID.setEditable(false);
		txtID.setBounds(63, 8, 86, 20);
		panel_1.add(txtID);
		txtID.setColumns(10);
		
		JLabel lblNewLabel_7 = new JLabel("Perfis:");
		lblNewLabel_7.setHorizontalAlignment(SwingConstants.TRAILING);
		lblNewLabel_7.setBounds(10, 61, 46, 14);
		panel_1.add(lblNewLabel_7);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(63, 58, 315, 82);
		panel_1.add(scrollPane_1);
		
		txtPerfisUsuario = new JTextArea();
		txtPerfisUsuario.setEditable(false);
		scrollPane_1.setViewportView(txtPerfisUsuario);
		
		txtToken = new JTextField();
		txtToken.setEditable(false);
		txtToken.setBounds(152, 8, 226, 20);
		panel_1.add(txtToken);
		txtToken.setColumns(10);
		
		JLabel lblNewLabel_4 = new JLabel("Usu\u00E1rio Logado");
		lblNewLabel_4.setBounds(265, 202, 127, 14);
		pnlAcoes.add(lblNewLabel_4);
		
		JScrollPane scrollPane_2 = new JScrollPane();
		scrollPane_2.setBounds(342, 72, 307, 122);
		pnlAcoes.add(scrollPane_2);
		
		txtRetornoSolicitacao = new JTextArea();
		scrollPane_2.setColumnHeaderView(txtRetornoSolicitacao);
		
		// Encerra o Agente de manuten��o quando usu�rio fecha o Frame	
		addWindowListener(new	WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				meuAgente.doDelete();
			}
		} );
	}
	
	public JTextArea getTxtPerfisUsuario() {
		return txtPerfisUsuario;
	}

	public void setTxtPerfisUsuario(JTextArea txtPerfisUsuario) {
		this.txtPerfisUsuario = txtPerfisUsuario;
	}

	public JTextField getTxtNome() {
		return txtNome;
	}

	public void setTxtNome(JTextField txtNome) {
		this.txtNome = txtNome;
	}

	public JTextField getTxtID() {
		return txtID;
	}

	public void setTxtID(JTextField txtID) {
		this.txtID = txtID;
	}

	public JTextField getTxtLocalTDB() {
		return txtLocalTDB;
	}

	public void setTxtLocalTDB(JTextField txtLocalTDB) {
		this.txtLocalTDB = txtLocalTDB;
	}

	public void showGui() {
		//pack();
		//setSize(674, 443);
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		int centerX = (int)screenSize.getWidth() / 2;
		int centerY = (int)screenSize.getHeight() / 2;
		setLocation(centerX - getWidth() / 2, centerY - getHeight() / 2);
		super.setVisible(true);
	}
}
